package ar.edu.unicen.exa.isistan.archassis.view.classesmap;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

import swing2swt.layout.BorderLayout;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.model.listeners.ISRCFolderClassesListener;
import ar.edu.unicen.exa.isistan.archassis.view.classes.HTMLClassComposite;

public class SRCFolderClassesComposite extends Composite implements ISRCFolderClassesListener {
	private Combo combo;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SRCFolderClassesComposite(SashForm parentSashForm) {
		super(parentSashForm, SWT.NONE);
		setLayout(new BorderLayout(0, 0));
		
		Composite srcFolderComposite = new Composite(this, SWT.NONE);
		srcFolderComposite.setLayoutData(BorderLayout.NORTH);
		srcFolderComposite.setLayout(new BorderLayout(0, 0));
		
		combo = new Combo(srcFolderComposite, SWT.READ_ONLY);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				onComboSelected();
				
			}
		});
		combo.setItems(new String[] {""});
		//combo.setText("Selecciones la Source Folder");
//		
	
		//Composite classesComposite = new ClassesComposite(this);
		Composite classesComposite = new HTMLClassComposite(this);
		classesComposite.setLayoutData(BorderLayout.CENTER);
		classesComposite.setLayout(new FillLayout(SWT.HORIZONTAL));

		//new ClassesComposite(classesComposite);
		buildCombo();
		
//		ModelManager.getInstance().setSRCFolderClassesListener(this);
	}

	protected void onComboSelected() {
		System.out.println(getCombo().getText());
//		List<JavaClass> ca = null;
//		if (srcJavaClassMap != null) {
//			ca = srcJavaClassMap.get(getCombo().getText());
//		}
//		ModelManager.getInstance().setClassesList(getCombo().getText());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	private Map<String, List<JavaClass>> srcJavaClassMap;
	@Override
	public void change(Map<String, List<JavaClass>> srcJavaClassMap) {
		this.srcJavaClassMap = srcJavaClassMap;
		getCombo().removeAll();
		if (this.srcJavaClassMap != null) {
			for (String src : this.srcJavaClassMap.keySet()) {
				getCombo().add(src);
			}
		} 
		buildCombo();
	}
	
	private void buildCombo() {
		if (combo.getItemCount() == 0) {
			combo.select(-1);
			combo.setText("Nada para mostrar");
			combo.setEnabled(false);
		} else {
			combo.select(-1);
			combo.setText("Selecciones la Source Folder");
			combo.setEnabled(true);
		}
	}
	
	protected Combo getCombo() {
		return combo;
	}
}
